package pe.com.rix.taglib.bean.menu;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MenuItem {

	private int id;
	private String name;
	private String url;
	private String icon;
	private List<MenuItem> items;
	
	public MenuItem() {
		this.items = new ArrayList<MenuItem>();
	}
	
	public MenuItem(int id, String name) {
		this.id = id;
		this.name = name;
		this.items = new ArrayList<MenuItem>();
	}
	
	public MenuItem(int id, String name, String url) {
		this.id = id;
		this.name = name;
		this.url = url;
		this.items = new ArrayList<MenuItem>();
	}
	
	public MenuItem(String name) {
		this.name = name;
		this.items = new ArrayList<MenuItem>();
	}
	
	public MenuItem(String name, boolean allowChilds) {
		this.name = name;
		if(allowChilds==true)
			this.items = new ArrayList<MenuItem>();
	}
	
	public MenuItem(String name, String url, String icon, boolean allowChilds) {
		this.name = name;
		this.url = url;
		this.icon = icon;
		if(allowChilds==true)
			this.items = new ArrayList<MenuItem>();
	}
	
	public void setValues(String[][] structure){
		List<MenuItem> itemsInLine = new ArrayList<MenuItem>();
		for(String[] menusrc : structure){
			int id = Integer.parseInt(menusrc[0]);
			String name = menusrc[1];
			int parentId = Integer.parseInt(menusrc[2]);
			if(parentId==0){
				MenuItem parentItem = null;
				if(menusrc[3]==null)
					parentItem =  new MenuItem(id, name);
				else
					parentItem =  new MenuItem(id, name, menusrc[3]);
				itemsInLine.add(parentItem);
				items.add(parentItem);
			}else{
				MenuItem parentItem = null;
				Iterator<MenuItem> i = itemsInLine.iterator();
				while(i.hasNext()){
					MenuItem m = i.next();
					if(m.getId()==parentId){
						parentItem = m;
						break;
					}
				}
				MenuItem menuItem = new MenuItem(id, name, menusrc[3]);
				System.out.println("menutItem" + menuItem.getId() + " ----> " + menuItem.getUrl());
				if( parentItem != null){
					itemsInLine.add(menuItem);
					parentItem.add(menuItem);
				}else{
					itemsInLine.add(menuItem);
					items.add(menuItem);
				}
				
			}
			
		}
		
	}
	
	
	public static void doHtml(List<MenuItem> items, StringBuffer html){
		
		for(MenuItem i : items){
			System.out.println(i.getName());
			System.out.println(i.getUrl());
			html.append("<li>");
			if( i.getUrl()!=null && i.getUrl().trim().length()>0){
				
				String url = i.getUrl();
				url = "javascript: doMenu("+url+");";
				
				html.append("<a href='" + url + "'>");
				html.append(i.getName());
				html.append("</a>");
			} else
				html.append(i.getName());

			if(i.getItems()!=null && i.getItems().size()>0){
				html.append("<ul>");
				doHtml(i.getItems(), html);
				html.append("</ul>");
			}
			
            html.append("</li>");
		}
		
	}
	
	public MenuItem getItem(int id){
		Iterator<MenuItem> i = items.iterator();
		while(i.hasNext()){
			MenuItem m = i.next();
			if(m.getId()==id)
				return m;
		}
		return null;
	}
	
	public void add(MenuItem menuItem){
		items.add(menuItem);
	}
	
	public List<MenuItem> getItems (){
		return items;
	}
	
	public String getName() {
		return name;
	}
	public MenuItem setName(String name) {
		this.name = name;
		return this;
	}
	public String getUrl() {
		return url;
	}
	public MenuItem setUrl(String url) {
		this.url = url;
		return this;
	}
	public String getIcon() {
		return icon;
	}
	public MenuItem setIcon(String icon) {
		this.icon = icon;
		return this;
	}
	
	public int getId() {
		return id;
	}
	
	
}
